//
//  FallLayer.cpp
//  slushy
//
//  Created by luotianqiang on 17/6/4.
//
//

#include "FallLayer.h"
#include "FoodData.h"
#include "particleLayer.h"
#include "DecorationLayer.h"
IMPLEMENT_COCOS2DX_CLASS(FallLayer);
REGIST_SECENE(FallLayer)
bool FallLayer::init(){
    if(BaseMakeLayer::init()) {
        _operate = StudioLayer::create("FallLayer.csb");
        addChild(_operate);
        _prompt = _operate->getNodeByName<Sprite*>("finger");
        vector<string> hideNodes = {"bigcup","bowl","bowlUp","fallSprite","cup_shadow","lid"};
        for(auto name:hideNodes)
            _operate->getNodeByName(name)->setVisible(false);
        return true;
    }
    return false;
}

void FallLayer::touchEnd(ui::Widget* widget){
    
}


void FallLayer::onEnterTransitionDidFinish(){
    BaseMakeLayer::onEnterTransitionDidFinish();
    this->showBowl();
    SoundPlayer::getInstance()->playEffect("sound/general/ingredients_fly_in.mp3");
    _operate->getNodeByName("cup_shadow")->runAction(Sequence::create(DelayTime::create(0.65f), FadeOut::create(0),Show::create(),FadeIn::create(0.2f), nullptr));
    ActionHelper::delayFunc(0.3f, this, [this](){
        SoundPlayer::getInstance()->playEffect("sound/general/ingredients_fly_in.mp3");
        auto bigcup = _operate->getNodeByName("bigcup");
        bigcup->setVisible(true);
        auto bigcupUp = _operate->getNodeByName("bigcupUp");
        auto clip = ClippingNode::create();
        clip->setAlphaThreshold(0.01f);
        bigcupUp->addChild(clip,-1);
        auto mask = Sprite::create("PNG/5/blender_cup_mask.png");
        mask->setPosition(bigcupUp->getContentSize()*.5);
        clip->setStencil(mask);
        auto mixPath = FoodData::getMixPath();
        auto node = Sprite::create(mixPath[0]);
        node->setName("flavorInBigCup");
        node->setPosition(137.51,80);
        clip->addChild(node);
        ActionHelper::showBezier(bigcup, bigcup->getPosition(), ActionHelper::ShowDirection::show_from_right,[=](){
            auto drag = createDraOperate(bigcup);
            drag->setNeedGravity(true);
            ActionHelper::runBezierMoveHideAction(_prompt, _operate->getPosinWord(bigcup->getName()), _operate->getPosinWord("limit"));
            
        });
        
        
    });
}

void FallLayer::startFall(LQComponent* fall, OperateListner* _lis){
    BaseMakeLayer::startFall(fall, _lis);
    fall->getOwner()->runAction(MoveBy::create(2, Vec2(0,50)));
    auto fallPath = FoodData::getFallPath();
    auto fallSprite = _operate->getNodeByName<Sprite*>("fallSprite");
    fallSprite->setTexture(fallPath[0]);
    auto fallAnimation = Animation::create();
    fallAnimation->setLoops(-1);
    fallAnimation->setDelayPerUnit(0.15f);
    for(auto str:fallPath)
        fallAnimation->addSpriteFrameWithFile(str);
    fallSprite->runAction(Animate::create(fallAnimation));
    fallSprite->setScale(0, 1);
    fallSprite->setVisible(true);
    fallSprite->runAction(ScaleTo::create(0.8f, 1));
    _operate->getNodeByName("flavorInBigCup")->runAction(Sequence::create( MoveBy::create(6, Vec2(0,-260)),CallFunc::create(CC_CALLBACK_0(LQComponent::dispatcherEvent, fall,FallThingfallEnd)),nullptr));
    auto inCupPaths = FoodData::getInCupPath();
    vector<Node*> cupNodes = {_operate->getNodeByName("bowl"),_operate->getNodeByName("bowlUp")};
    for(int i =0;i<2;i++){
        auto clip = ClippingNode::create();
        clip->setAlphaThreshold(0.01f);
        cupNodes[i]->addChild(clip,i*-1);
        auto mask = Sprite::create(inCupPaths[0]);
        mask->setPosition(cupNodes[i]->getContentSize()*.5);
        clip->setStencil(mask);
        auto mixPath = FoodData::getMixPath();
        auto node = Sprite::create(inCupPaths[i]);
        node->setPosition(cupNodes[i]->getContentSize().width*.5f,-220);
        clip->addChild(node);
        node->runAction(MoveTo::create(6, cupNodes[i]->getContentSize()*.5));
    }
}
void FallLayer::fallEnd(LQComponent* fall, OperateListner* _lis){
    BaseMakeLayer::fallEnd(fall, _lis);
    auto fallSprite = _operate->getNodeByName<Sprite*>("fallSprite");
    fallSprite->stopAllActions();
    fallSprite->runAction(ScaleTo::create(0.4f, 0,1));
    hideThing(fall->getOwner(),0.4f);
    ActionHelper::delayFunc(0.7f, this, [this](){
        
        SoundPlayer::getInstance()->playEffect("sound/general/ingredients_fly_in.mp3");
        auto lid = _operate->getNodeByName("lid");
        lid->setVisible(true);
        ActionHelper::show(lid, lid->getPosition(), ActionHelper::ShowDirection::show_from_right,[=](){
            auto touchMove = TouchMoveComponent::create();
            lid->addComponent(touchMove);
            touchMove->setMoveType(MoveComponentType::kTypeBack);
            touchMove->setTarget(LQRect::create(CocosHelper::getNodeRectInWorld(_operate->getNodeByName("limit"))));
            ActionHelper::runBezierMoveHideAction(_prompt, _operate->getPosinWord(lid->getName()),  _operate->getPosinWord("limit"));
            touchMove->setBrintTop(true);
            touchMove->addListners(ComponentTouchEnd, [=](LQComponent*,OperateListner*){
                this->stopPrompt();
                auto f = FrameStatus::create(lid->getName());
                f->setSearchAction(_operate->getAction());
                auto endPos = f->searchPostion("pouring");
                f->remove();
                lid->runAction(Sequence::create(JumpTo::create(0.8f, endPos, 100, 1),DelayTime::create(0.5f), CallFunc::create([=](){
                    CocosHelper::reAddNode(lid, _operate->getNodeByName("bowlUp"));
                    _operate->getNodeByName("cup_shadow")->setVisible(false);
                    auto bowl = _operate->getNodeByName("bowl");
                    auto bowlUp = _operate->getNodeByName("bowlUp");
                    CocosHelper::reAddNode(bowlUp, bowl);
                    bowlUp->setName("");
                    bowlUp->stopAllActions();
                    
                    bowl->runAction(Sequence::create(ScaleTo::create(0.9f, 1.25f),CallFunc::create([=](){
                        this->addChild(particleLayer::create(),10);
                    }),DelayTime::create(1.5),CallFunc::create([this](){
                        Director::getInstance()->replaceScene(TransitionFade::create(1.2f, DecorationLayer::createScene(), Color3B::WHITE));
                    }), nullptr));
                    bowl->runAction(JumpTo::create(0.9f, bowl->getParent()->convertToNodeSpace(getContentSize()*.5), 200, 1));
                }), nullptr));
                ActionHelper::delayFunc(0.6, this, [=](){
                    touchMove->playSound();
                });
            });
        });
        
    
    });
}
void FallLayer::onExitTransitionDidStart(){
    BaseMakeLayer::onExitTransitionDidStart();
    
}